package ninja.awesome.framework.impl;

import ninja.awesome.R;
import ninja.awesome.framework.Audio;
import ninja.awesome.framework.FileIO;
import ninja.awesome.framework.Game;
import ninja.awesome.framework.Graphics;
import ninja.awesome.framework.Input;
import ninja.awesome.framework.Screen;
import ninja.awesome.game.AdListenerImpl;
import ninja.awesome.game.Assets;
import ninja.awesome.game.MainGameScreen;
import ninja.awesome.game.MainMenuScreen;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.text.InputType;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public abstract class AndroidGame extends Activity implements Game {
	protected AndroidFastRenderView renderView;
	Graphics graphics;
	Audio audio;
	Input input;
	FileIO fileIO;
	Screen screen;
	WakeLock wakeLock;

	// Screensize
	public static final int WIDTH = 1280;
	public static final int HEIGHT = 720;
	public static final boolean ENABLE_SOUND = true;
	public static int ROAD_Y = 420;
	public static int ROAD_HEIGHT = 203;
	private RelativeLayout gameContainer;
	private AdView adView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
		// int frameBufferWidth = isPortrait ? 800 : 1280;
		// int frameBufferHeight = isPortrait ? 1280 : 800;
		int frameBufferWidth = WIDTH;
		int frameBufferHeight = HEIGHT;
		Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Config.RGB_565);

		float scaleX = (float) frameBufferWidth / getWindowManager().getDefaultDisplay().getWidth();
		float scaleY = (float) frameBufferHeight / getWindowManager().getDefaultDisplay().getHeight();

		renderView = new AndroidFastRenderView(this, frameBuffer);
		graphics = new AndroidGraphics(getAssets(), frameBuffer);
		fileIO = new AndroidFileIO(this);
		audio = new AndroidAudio(this);
		input = new AndroidInput(this, renderView, scaleX, scaleY);

		setContentView(R.layout.activity_main);
		gameContainer = (RelativeLayout) findViewById(R.id.game_container);
		gameContainer.addView(renderView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

		adView = (AdView) findViewById(R.id.adView);
		adView.setAdListener(new AdListenerImpl(adView));
		adView.loadAd(new AdRequest.Builder().build());
		PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
		wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");
		Assets.fontFace = Typeface.createFromAsset(getResources().getAssets(), "dosisbold.ttf");
		setScreen(getInitScreen());
	}

	@Override
	public void onResume() {
		super.onResume();
		wakeLock.acquire();
		screen.resume();
		renderView.resume();
	}

	@Override
	public void onPause() {
		adView.pause();
		wakeLock.release();
		renderView.pause();
		screen.pause();

		if (isFinishing())
			screen.dispose();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		adView.destroy();
		super.onDestroy();
	}

	@Override
	public Input getInput() {
		return input;
	}

	@Override
	public FileIO getFileIO() {
		return fileIO;
	}

	@Override
	public Graphics getGraphics() {
		return graphics;
	}

	@Override
	public Audio getAudio() {
		return audio;
	}

	@Override
	public void setScreen(Screen screen) {
		if (screen == null)
			throw new IllegalArgumentException("Screen must not be null");
		if (this.screen != null) {
			this.screen.pause();
			this.screen.dispose();
		}
		screen.resume();
		screen.update(0);
		this.screen = screen;
	}

	@Override
	public Screen getCurrentScreen() {
		return screen;
	}

	@Override
	public void onBackPressed() {
		if (this.screen instanceof MainMenuScreen) {
			this.screen.backButton();
			super.onBackPressed();
		} else {
			this.screen.backButton();
		}
	}

	public void setAdVisibility(Boolean show) {
		if (show) {
			adView.setVisibility(View.VISIBLE);
		} else {
			adView.setVisibility(View.GONE);
		}
	}

	public void processPlay() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		final EditText ed = new EditText(this);
		ed.setHint(R.string.player_name_hint);
		ed.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
		alert.setView(ed);
		alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int whichButton) {
				String playerName = ed.getText().toString().trim();
				if (playerName.length() < 6) {
					Toast.makeText(ed.getContext(), "Minimun 6 characters is required", Toast.LENGTH_SHORT).show();
					return;
				}
				if (playerName.length() > 12) {
					Toast.makeText(ed.getContext(), "Maximum 12 characters is required", Toast.LENGTH_SHORT).show();
					return;
				}
				if (!playerName.matches("[a-zA-Z0-9 ]*")) {
					Toast.makeText(ed.getContext(), "Only accept Alphabet character", Toast.LENGTH_SHORT).show();
					return;
				}

				MainGameScreen gameScreen = new MainGameScreen(AndroidGame.this);
				gameScreen.setPlayerName(playerName);
				AndroidGame.this.setScreen(gameScreen);
			}
		});

		alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int whichButton) {
			}
		});
		alert.show();
	}
}